var JobActions = {
   actions: {},
   defaults: {},
   icons: {},
   currentJob: null,
   register: function (mime, name, permissions, icon, action) {
      if (!JobActions.actions[mime]) {
         JobActions.actions[mime] = {};
      }
      if (!JobActions.actions[mime][name]) {
         JobActions.actions[mime][name] = {};
      }
      JobActions.actions[mime][name]['action'] = action;
      JobActions.actions[mime][name]['permissions'] = permissions;
      JobActions.icons[name] = icon;
   },
   setDefault: function (mime, name) {
      JobActions.defaults[mime] = name;
   },
   get: function (mime, type, permissions) {
      var actions = {};
      if (JobActions.actions.all) {
         actions = $.extend(actions, JobActions.actions.all);
      }
      if (mime) {
         if (JobActions.actions[mime]) {
            actions = $.extend(actions, JobActions.actions[mime]);
         }
         var mimePart = mime.substr(0, mime.indexOf('/'));
         if (JobActions.actions[mimePart]) {
            actions = $.extend(actions, JobActions.actions[mimePart]);
         }
      }
      if (type) {//type is 'dir' or 'file'
         if (JobActions.actions[type]) {
            actions = $.extend(actions, JobActions.actions[type]);
         }
      }
      var filteredActions = {};
      $.each(actions, function (name, action) {
         if (action.permissions & permissions) {
            filteredActions[name] = action.action;
         }
      });
      return filteredActions;
   },
   getDefault: function (mime, type, permissions) {
      if (mime) {
         var mimePart = mime.substr(0, mime.indexOf('/'));
      }
      var name = false;
      if (mime && JobActions.defaults[mime]) {
         name = JobActions.defaults[mime];
      } else if (mime && JobActions.defaults[mimePart]) {
         name = JobActions.defaults[mimePart];
      } else if (type && JobActions.defaults[type]) {
         name = JobActions.defaults[type];
      } else {
         name = JobActions.defaults.all;
      }
      var actions = this.get(mime, type, permissions);
      return actions[name];
   },
   getCurrentId: function() {
      return  JobActions.currentJob.attr('data-id'); 
   }, 

   getCurrentJobName: function () {
      return JobActions.currentJob.attr('data-name');
   },
   getCurrentMimeType: function () {
      return JobActions.currentJob.attr('data-mime');
   },
   getCurrentType: function () {
      return JobActions.currentJob.attr('data-type');
   },
   getCurrentPermissions: function () {
      return JobActions.currentJob.attr('data-permissions');
   },
   getCurrentSavedStatus: function () {
      return JobActions.currentJob.attr('data-saved');
   },
   getCurrentTrashedStatus: function () {
      return JobActions.currentJob.attr('data-trashed');
   }
};

$(document).ready(function () {
   $('#jobneeds .jobitem .jobactions').on("click", "a.action", function(event) {
      event.stopPropagation();
      event.preventDefault();

      var action = $(this).data("action");
      var jobitem = $(this).closest('.jobitem');
      
      if(action == 'Trash' && !(jobitem.data('permissions') & OC.PERMISSION_DELETE)) {
         showMessagePanel('您没有该职位权限', $(this), true, {placement:'left'});
      }
      
      JobActions.currentJob = jobitem;
      var job = JobActions.getCurrentId();
      var actions = JobActions.get(JobActions.getCurrentMimeType(), JobActions.getCurrentType(), JobActions.getCurrentPermissions());
      if($.isFunction(actions[action])) {
         actions[action](job);
      }

      return false; 
   }); 
});

JobActions.register('all', 'Save', OC.PERMISSION_READ, function () {
   return OC.imagePath('core', 'actions/add');
}, function (job) {
   JobList.do_save(job);
});

JobActions.register('all', 'Unsave', OC.PERMISSION_READ, function () {
   return OC.imagePath('core', 'actions/add');
}, function (job) {
   JobList.do_unsave(job);
});

JobActions.register('all', 'Settop', OC.PERMISSION_READ, function () {
   return OC.imagePath('core', 'actions/add');
}, function (job) {
   JobList.do_settop(job);
});

JobActions.register('all', 'Unsettop', OC.PERMISSION_READ, function () {
   return OC.imagePath('core', 'actions/add');
}, function (job) {
   JobList.do_unsettop(job);
});

JobActions.register('all', 'Restore', OC.PERMISSION_DELETE, function () {
   return OC.imagePath('core', 'actions/undelete.png');
}, function (job) {
   JobList.do_restore(job);
});

JobActions.register('all', 'Trash', OC.PERMISSION_DELETE, function () {
   return OC.imagePath('core', 'actions/delete');
}, function (job) {
   if(typeof(storageGet('iknow_jobtrash_tips')) != 'undefined') {
      JobList.do_trash(job);
   } else {
      showJobTrashConfirm(function(){
         JobList.do_trash(job);
      });
   }
});

JobActions.setDefault('dir', 'Open');
